package rsj.proxy.bind;

import rsj.proxy.parse.ParamTypeEnum;
import rsj.proxy.parse.SqlSource;

import java.lang.reflect.Method;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 默认sql参数绑定
 *
 * @author yuanqinglong
 * @date 2021/05/24
 * @since 2021/5/24 11:03
 */
public abstract class AbstractParamResolveFactory {


    /**
     * 处理各种参数类型对应解析器
     */
    protected final Map<ParamTypeEnum, ParamResolveProcessor> paramResolveProcessorMap;


    /**
     * 参数解析器工厂
     *
     * @param paramResolveProcessors 参数解决处理器
     */
    protected AbstractParamResolveFactory(List<ParamResolveProcessor> paramResolveProcessors) {
        paramResolveProcessorMap = paramResolveProcessors.stream().collect(Collectors.toMap(ParamResolveProcessor::getSupportType, Function.identity(),
                (paramResolveProcessor, paramResolveProcessor2) -> paramResolveProcessor, () -> new EnumMap<>(ParamTypeEnum.class)));
    }

    /**
     * 解析绑定参数
     *
     * @param method    方法
     * @param args      arg游戏
     * @param sqlSource sql源
     * @return {@link Object[]}
     * @throws Exception 异常
     */
    public abstract Object[] doParameterAnalysis(SqlSource sqlSource, Method method, Object[] args) throws Exception;


}
